#include "Gk.h"

#ifdef GK_DEVICE_DIRECTX9

namespace gk
{
	void Buffer::Lock()
	{
		switch (m_format)
		{
		case BufferFormat::VertexStream:
			{
				m_internal.m_pVertexBuffer->Lock(0, m_stride * m_numElements, &m_internal.m_pWriteAddress, 0);
				break;
			}
		case BufferFormat::Index16Stream:
		case BufferFormat::Index32Stream:
			{
				m_internal.m_pIndexBuffer->Lock(0, m_stride * m_numElements, &m_internal.m_pWriteAddress, 0);
				break;
			}
		default:
			{
				m_internal.m_pWriteAddress = m_internal.m_pGenericBuffer;
				break;
			}
		}
	}

	void Buffer::Write(void* pData, cr::size_t dataSize)
	{
		CR_ASSERT(m_internal.m_pWriteAddress != 0);
		CR_ASSERT(dataSize <= (m_stride * m_numElements));
		memcpy(m_internal.m_pWriteAddress, pData, dataSize);
	}

	void Buffer::Unlock()
	{
		switch (m_format)
		{
		case BufferFormat::VertexStream:
			{
				m_internal.m_pVertexBuffer->Unlock();
				break;
			}
		case BufferFormat::Index16Stream:
		case BufferFormat::Index32Stream:
			{
				m_internal.m_pIndexBuffer->Unlock();
				break;
			}
		default:
			{
				break;
			}
		}

		m_internal.m_pWriteAddress = NULL;
	}
}

#endif // GK_DEVICE_DIRECTX9